"""
Case Type   : 进程
Case Name   : 非连接客户端/连接客户端host字段的显示
Create At   : 2024.10.26
Owner       : l1azzzy
Description :
    1、创建兼容B库;
    2、切换至B库,查看进程
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:创建兼容B库;expect:成功----'
        db_testname = 'd_compatibility_desc_a'
        self.log.info(text)
        sql_cmd = f'create database d_compatibility_desc_a dbcompatibility=\'B\';'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('Error', sql_res, '执行失败' + text)
        text2 = '----step2:关闭线程池;expect:成功----'
        self.log.info(text2)
        sql_cmd2 = f'ALTER SYSTEM SET enable_thread_pool to off;'
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=db_testname, dbuser={node.ssh_user})
        self.log.info(sql_res2)
        self.assertNotIn('Error', sql_res2, '执行失败' + text2)
        text3 = '----step3:重启数据库 expect:成功----'
        self.log.info(text3)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text3)
        text4 = '----step4:查询会话信息;expect:成功----'
        self.log.info(text4)
        sql_cmd4 = f'select * from pg_stat_activity;'
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4, dbname=db_testname)
        self.log.info(sql_res4)
        self.assertIn('default_pool', sql_res4, '执行失败' + text4)
        
        self.log.info(f'-----{os.path.basename(__file__)} end-----')